// pages/read/read.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    content:'',
    bookId:'',
    openid: '',
    index:1,
    bookName:'',
    total:'',
    loadingHidden:'true',/*设置下边按钮在内容出来后显示 */
    isDisabledLast: true,
    isDisabledNext:false  
  },
  handleInput(e) {
    var that=this;
    let value = that.validateNumber(e.detail.value)
    that.setData({
      value
    })
  },
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },
  requestpage: function (event){
    var that=this;
    var type = event.currentTarget.dataset.keyword;
    var total = that.data.total;
    /*上一页  下一页*/
    if(type=="last"){
      that.setData({
        index: that.data.index - 1,
        isDisabledNext: false,
      })
      if (that.data.index==1){
        that.setData({
          isDisabledLast: true
        })
      }
      wx.request({
        url: app.globalData.serverIp + 'book/getBookIndex.json',
        data: {
          'index': that.data.index,
          'bookId': that.data.bookId,
          'openid': that.data.openid
        },
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          /*设置滚动条回到最上方 */
          wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
          });
          that.setData({
            content: res.data.data.sendData.content,
            loadingHidden: false,
            total: res.data.data.sendData.total
          })
        }
      })
      wx.request({
        url: app.globalData.serverIp + 'records/update.json',
        data: {
          'index': that.data.index,
          'bookId': that.data.bookId,
          'openid': that.data.openid
        },
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
        }
      })
    }else{
      that.setData({
        isDisabledLast: false,
        index: that.data.index-0+1    
      })
      if (that.data.index == total) {
        that.setData({
          isDisabledNext: true
        })
      }
      wx.request({
        url: app.globalData.serverIp + 'book/getBookIndex.json',
        data: {
          'index': that.data.index,
          'bookId': that.data.bookId,
          'openId': that.data.openid
        },
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
          })
          that.setData({
            content: res.data.data.sendData.content,
            loadingHidden: false,
            total: res.data.data.sendData.total
          })
         
        }
      })
      wx.request({
        url: app.globalData.serverIp + 'records/update.json',
        data: {
          'index': that.data.index,
          'bookId': that.data.bookId,
          'openId': that.data.openid
        },
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
        }
      })
    }
  },
  go:function(){
    var that=this;
    wx.request({
      url: app.globalData.serverIp + 'book/getBookIndex.json',
      data: {
        'index': that.data.value,
        'bookId': that.data.bookId,
        'openid': that.data.openid
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        /*设置滚动条回到最上方 */
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 0
        });
        that.setData({
          index:that.data.value-0,
          content: res.data.data.sendData.content,
          loadingHidden: false,
          total: res.data.data.sendData.total
        })
        console.log()
        if(that.data.index==1){
          that.setData({
            isDisabledLast: true,
            isDisabledNext: false
          })
        } else if (that.data.index == that.data.total){
          that.setData({
            isDisabledNext: true,
            isDisabledLast: false
          })
        }else{
          that.setData({
            isDisabledNext: false,
            isDisabledLast: false
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var openid = wx.getStorageSync('openid'); //获取本地缓存中的userIdEnc //用户唯一识别码
    that.setData({
      bookId: options.bookId,
      bookName: options.bookName,
      openid:openid,
      index: options.index
    })
    wx.request({
      url: app.globalData.serverIp + 'book/getBookIndex.json',
      data: {
        'index': that.data.index,
        'bookId': that.data.bookId,
        'openid': that.data.openid
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          content: res.data.data.sendData.content,
          loadingHidden:false,
          total: res.data.data.sendData.total
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})